﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;
using DevExpress.XtraPrinting;
using Stimulsoft.Report;
using Stimulsoft.Report.Export;
using System.IO;
using Stimulsoft.Report.Dictionary;


namespace Gemciler_Uretim.SiparişFormları
{
    public partial class _700_Irsaliye_Siparisler : DevExpress.XtraEditors.XtraForm
    {
        public _700_Irsaliye_Siparisler()
        {
            InitializeComponent();
        }
       
        SqlConnecitonClass sqlbaglanti = new SqlConnecitonClass();
        Int32 secilen_RECno = 0, secilen_irsaliye_sira = 0; // 1-bekleyen 4-Onaylı 5-iptaller 6-Uretim 7-
        string secilen_baslama_tarihi, secilen_teslim_tarihi, secilen_bolum, secilen_sip_müşterisi, secilen_sip_no, uretim_bolum_no, uretim_bolum_adi, secilen_irsaliye_seri, irsaliye_cari_kodu;
        public string detay_ekrani_acma_sekli, par_secim_sarti;
        public int aktif_kullanici, ayarlanacak_ekran_no;
        
        public int[] param_700 = new int[100];
        string detay_tipi = "uretim", ilktarih, sontarih, birgunonce;
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        public string ku_mail_hesap, ku_mail_sifre, ku_mail_pop3, ku_mail_unvani, kime1, sqlconnectstr;
        public Int32 mail_port = 587;
        string AppPath = System.Windows.Forms.Application.StartupPath;
     
        private void BekleyenSiparisler_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.Text = "" + ayarlanacak_ekran_no + "-İrsaliyeler/ Aktif Bölümler:" + par_secim_sarti + "";
            barEditItem1.EditValue = DateTime.Today;
            barEditItem2.EditValue = DateTime.Today;
            vade_ayarla();
            _1_dgv_guncelle();
          
            Yetkileri_yukle();
          
        }
        private void Yetkileri_yukle()
        {
            
            _701_contextMenuıIRSALIYE.Enabled = false;
            _700_mailGonderToolStripMenuItem.Enabled = false;
            _702_toolStripMenuItem3.Enabled = false;

            if (param_700[0] == 0) { _700_mailGonderToolStripMenuItem.Enabled = true; }
            if (param_700[1] == 0) { _701_contextMenuıIRSALIYE.Enabled = true; }
            if (param_700[2] == 0) { _702_toolStripMenuItem3.Enabled = true; }
            if (param_700[3] == 0) { }
            if (param_700[4] == 0) { }
             if (param_700[5]  == 0) { }
             if (param_700[6]  == 0) { }
             if (param_700[7] == 0) { }
             if (param_700[8] == 0) { }
             if (param_700[9] == 0) { }
             if (param_700[10] == 0) { }
             if (param_700[90] == 0) {}





        }
        private void _15_detayları_Guncelle()
        {

            gridControl2.DataSource = null;
            gridView2.Columns.Clear();
            gridControl2.DataSource = sqlbaglanti.ShowTable("SELECT * FROM tkfn_SevkEdilenİrsaliyelerDetay('" + secilen_irsaliye_seri + "' ," + secilen_irsaliye_sira + ")", CommandType.Text);

            this.gridControl2.TabIndex = 0;
            this.gridView2.OptionsBehavior.AllowIncrementalSearch = false;
            this.gridView2.OptionsView.ShowAutoFilterRow = false;
            this.gridView2.OptionsView.ShowGroupPanel = false;
            this.gridView2.OptionsBehavior.Editable = false;
            this.gridView2.OptionsBehavior.ReadOnly = false;
            this.gridView2.OptionsView.ColumnAutoWidth = false;
            this.gridView2.OptionsView.HeaderFilterButtonShowMode = FilterButtonShowMode.Button;
            this.gridView2.BestFitColumns();

        }
        private void _1_dgv_guncelle()
        {
            gridControl1.DataSource = null;
            gridView1.Columns.Clear();
            // ---sip_special1='O' VEYA '' ONAY BEKLİYOR 
            gridControl1.DataSource = sqlbaglanti.ShowTable("SELECT * FROM tkfn_SevkEdilenİrsaliyeler('" + ilktarih + "','" + sontarih + "')", CommandType.Text);
            this.gridControl1.TabIndex = 0;
            this.gridView1.OptionsBehavior.AllowIncrementalSearch = true;
            this.gridView1.OptionsView.ShowAutoFilterRow = true;
            this.gridView1.OptionsView.ShowGroupPanel = false; // gruplama alanı
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsBehavior.ReadOnly = false;
            this.gridView1.OptionsView.ShowFooter = true;
            gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            this.gridView1.ColumnPanelRowHeight = 55;

            gridView1.OptionsView.HeaderFilterButtonShowMode = FilterButtonShowMode.Button;
            //this.gridView1.Columns["TAMAMLANAN"].Visible = false;
            //this.gridView1.Columns["KALAN"].Visible = false;
            //this.gridView1.Columns["DEPO"].Visible = false;
            //this.gridView1.Columns["TESLİM ŞEKLİ"].Visible = false;
            //this.gridView1.Columns["KODU"].Visible = false;      
            //this.gridView1.Columns["REZERVE"].Visible = false;
            //this.gridView1.Columns["MİKTAR"].DisplayFormat.FormatType = FormatType.Numeric;
            //this.gridView1.Columns["MİKTAR"].DisplayFormat.FormatString = "n2";
            //this.gridView1.Columns["ÜRETİME BAŞLANACAK TARİH"].Width = 80;
            //gridView1.Columns["MİKTAR"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
            //gridView1.Columns["MİKTAR"].SummaryItem.DisplayFormat = "{0:N2}";
            //gridView1.Columns["MİKTAR"].SummaryItem.Tag = 1;
            //gridView1.Columns["İŞLEM DURUM"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            //gridView1.Columns["İŞLEM DURUM"].SummaryItem.DisplayFormat = "{0:N0} ADET";
            //gridView1.Columns["İŞLEM DURUM"].SummaryItem.Tag = 1;

            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                this.gridView1.Columns[i].OptionsFilter.FilterPopupMode = DevExpress.XtraGrid.Columns.FilterPopupMode.CheckedList;

            }

            gridView1.BestFitColumns();
        }
        private void _2_dgv_notlar_guncelle()
        {
            gridControl2.DataSource = null;
            gridView2.Columns.Clear();
            // ---sip_special1='O' VEYA '' ONAY BEKLİYOR 
            gridControl2.DataSource = sqlbaglanti.ShowTable("SELECT [not_RECno] AS KAYITNO      ,[not_sipRECno] AS [SİPNO]      ,[not_kullanici] AS KULNO  ,(SELECT ku_adi FROM TK_KULLANICI_TANIMLARI WHERE ku_kodu=not_kullanici)AS [KULLANICI ADI]    ,[not_date] AS [TARİH]"
            + " ,[not_detay] AS [NOT]  FROM [TK_SIPARIS_NOTLARI] where not_sipRECno=" + secilen_RECno + " ORDER BY not_date DESC  ", CommandType.Text);
            this.gridControl2.TabIndex = 0;
            this.gridView2.OptionsBehavior.AllowIncrementalSearch = false;
            this.gridView2.OptionsView.ShowAutoFilterRow = false;
            this.gridView2.OptionsView.ShowGroupPanel = false;
            this.gridView2.OptionsBehavior.Editable = false;
            this.gridView2.OptionsBehavior.ReadOnly = false;
            gridView2.OptionsView.ColumnAutoWidth = false;
            this.gridView2.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            gridView2.OptionsView.HeaderFilterButtonShowMode = FilterButtonShowMode.Button;
            this.gridView2.Columns["SİPNO"].Visible = false;
            this.gridView2.Columns["KAYITNO"].Visible = false;
            this.gridView2.Columns["TARİH"].DisplayFormat.FormatString = "DD-MM-YYYY SS:dd";
            this.gridView2.Columns["KULNO"].Visible = false;
            gridView2.BestFitColumns();
        }
        private void vade_ayarla()
        {
            string date1 = barEditItem1.EditValue.ToString();
            string date2 = barEditItem2.EditValue.ToString();

            string yil = date1.Substring(6, 4);
            string ay = date1.Substring(3, 2);
            string gun = date1.Substring(0, 2);
            ilktarih = yil + ay + gun;

            string yil1 = date2.Substring(6, 4);
            string ay1 = date2.Substring(3, 2);
            string gun1 = date2.Substring(0, 2);
            sontarih = yil1 + ay1 + gun1;



        }
        private void _9_ekran_kolonlarini_ayarla()
        {
            gridControl9.DataSource = sqlbaglanti.ShowTable("SELECT * FROM DBO.tkfn_KullaniciEkranKolonlari(" + ayarlanacak_ekran_no + "," + aktif_kullanici + ")  ORDER BY [KOLON NO]", CommandType.Text);
            for (int i = 0; i <= gridView9.RowCount - 1; i++)
            {
                try
                {
                    string kolon = Convert.ToString(gridView9.GetRowCellValue(i, "KOLON ADI").ToString());
                    this.gridView1.Columns[kolon].OptionsFilter.FilterPopupMode = DevExpress.XtraGrid.Columns.FilterPopupMode.CheckedList;
                }
                catch { }


                string sd_pasif = Convert.ToString(gridView9.GetRowCellValue(i, "PASİF").ToString());
                if (sd_pasif == "True")
                {
                    int index1 = gridView9.FocusedRowHandle;
                    string kolon = Convert.ToString(gridView9.GetRowCellValue(i, "KOLON ADI").ToString());
                    //Int16 format_tipi = Convert.ToInt16(gridView9.GetRowCellValue(i, "FORMAT").ToString());
                    //int format_tipi_alt_topla = Convert.ToInt16(gridView9.GetRowCellValue(i, "TOPLAM").ToString());
                    try
                    {
                        if (ayarlanacak_ekran_no == 700)
                        {
                            this.gridView1.Columns[kolon].Visible = false;
                            //if (format_tipi == 1)
                            //{
                            //    this.gridView1.Columns[kolon].DisplayFormat.FormatType = FormatType.Numeric;
                            //    this.gridView1.Columns[kolon].DisplayFormat.FormatString = "n2";
                            //}
                            //if (format_tipi == 2)
                            //{
                            //    this.gridView1.Columns[kolon].DisplayFormat.FormatType = FormatType.Numeric;
                            //    this.gridView1.Columns[kolon].DisplayFormat.FormatString = "n0";
                            //}
                            //if (format_tipi == 3)
                            //{
                            //    this.gridView1.Columns[kolon].DisplayFormat.FormatType = FormatType.Numeric;
                            //    this.gridView1.Columns[kolon].DisplayFormat.FormatString = "n2";
                            //}

                        }
                    }
                    catch { MessageBox.Show("KOLON YOK"); }
                }
                if (sd_pasif == "False")
                {
                    int index1 = gridView9.FocusedRowHandle;
                    string kolon = Convert.ToString(gridView9.GetRowCellValue(i, "KOLON ADI").ToString());
                    Int32 kolon_sira_no = Convert.ToInt32(gridView9.GetRowCellValue(i, "KOLON SIRASI").ToString());
                    try
                    {
                        if (ayarlanacak_ekran_no == 700) { this.gridView1.Columns[kolon].VisibleIndex = kolon_sira_no; }
                    }
                    catch { MessageBox.Show("KOLON YOK"); }
                }
            }
        }
        private void ekran_kolon_ayarlariniAc()
        {
            Ekran.EkranAyarı fr = new Ekran.EkranAyarı();
            fr.tablo_no = ayarlanacak_ekran_no;
            fr.kullanici_no = aktif_kullanici;
            fr.ShowDialog();
            _9_ekran_kolonlarini_ayarla();
        }
        private void FormYazdirAc()
        {
            Raporlar.RaporSec fr = new Raporlar.RaporSec();
            fr.aktif_kullanici = aktif_kullanici;
            fr.rapor_tablo_no = 700;
            fr.kayitno = secilen_RECno;
            fr.ShowDialog();
        }

        private bool SendMail(string to, string subject, string message, string name)
        {

            //try
            //{


                System.Net.NetworkCredential cred = new System.Net.NetworkCredential(ku_mail_hesap, ku_mail_sifre);
                mail.To.Add(to);
                mail.Subject = subject;
                mail.From = new System.Net.Mail.MailAddress(kime1, name);
                mail.IsBodyHtml = true;
                mail.Body = message;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(ku_mail_pop3, mail_port);
                smtp.UseDefaultCredentials = false;

                smtp.EnableSsl = false;
                MailFormuHazirla();   /// mail için raporu ekle cari kod belirledikten sonra
                smtp.Credentials = cred;
                //mail.Attachments.Add(new Attachment("c:\\temp\\MMM.txt"));
                //  mail.Attachments.Add(new Attachment("C:\\MMM.TXT", "application/octet-stream"));
                smtp.Send(mail);





            //}
            //catch (Exception)
            //{
            //    return false;
            //}
            return true;

        }
        private void MailFormuHazirla()
        {

            StiReport report = new StiReport();
           
            report.Load("" + AppPath + "\\Form\\IRSBILGI.MRT");
            report.Dictionary.Databases.Clear();
            report.Dictionary.Databases.Add(new StiSqlDatabase("RAPORDATA", "" + sqlconnectstr + ""));
            report["@evraknoseri"] = secilen_irsaliye_seri;
            report["@evraknosira"] = secilen_irsaliye_sira;
            report["@carikodu"] = irsaliye_cari_kodu;

           
           
            report.Render();

            StiPdfExportSettings settings = new StiPdfExportSettings();
            MemoryStream stream = new MemoryStream();
            MemoryStream ms = new MemoryStream();
            report.ExportDocument(StiExportFormat.Pdf, ms);
            ms.Seek(0, SeekOrigin.Begin);
            mail.Attachments.Add(new System.Net.Mail.Attachment(ms, "IRSBILGI.pdf", "application/pdf"));


        }
        private void MailFormuDZYN()
        {

            StiReport report = new StiReport();

            report.Load("" + AppPath + "\\Form\\IRSBILGI.MRT");
            report.Dictionary.Databases.Clear();
            report.Dictionary.Databases.Add(new StiSqlDatabase("RAPORDATA", "" + sqlconnectstr + ""));
            report["@evraknoseri"] = secilen_irsaliye_seri;
            report["@evraknosira"] = secilen_irsaliye_sira;
            report["@carikodu"] = irsaliye_cari_kodu;


            report.Design();
           


        }
        private void MailFormuYAZDIR()
        {

            StiReport report = new StiReport();

            report.Load("" + AppPath + "\\Form\\IRSBILGI.MRT");
            report.Dictionary.Databases.Clear();
            report.Dictionary.Databases.Add(new StiSqlDatabase("RAPORDATA", "" + sqlconnectstr + ""));
            report["@evraknoseri"] = secilen_irsaliye_seri;
            report["@evraknosira"] = secilen_irsaliye_sira;
            report["@carikodu"] = irsaliye_cari_kodu;


            report.ShowWithWpf();



        }
        private void gridView1_Click(object sender, EventArgs e)
        {
            try
            {
                int index1 = gridView1.FocusedRowHandle;
              secilen_irsaliye_sira = Convert.ToInt32(gridView1.GetRowCellValue(index1, "SIRA").ToString());
                kime1= Convert.ToString(gridView1.GetRowCellValue(index1, "MAİL").ToString());
                irsaliye_cari_kodu=Convert.ToString(gridView1.GetRowCellValue(index1, "KODU").ToString());
                secilen_irsaliye_seri= Convert.ToString(gridView1.GetRowCellValue(index1, "SERİ").ToString());
              _15_detayları_Guncelle();


            }
            catch { }
        }

       

      

        private void _208_toolStripMenuItem2_Click(object sender, EventArgs e)
        {
           
            FormYazdirAc();
        }

        private void _209_toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
            ekran_kolon_ayarlariniAc();
        }

        private void yenileToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            _1_dgv_guncelle();
            _2_dgv_notlar_guncelle();
        }

       

        private void yenileToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            _2_dgv_notlar_guncelle();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            vade_ayarla();
            _1_dgv_guncelle();

        }

        private void mailGonderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            kime1 = kime1 + "," + ku_mail_hesap;

            if (SendMail(kime1, "GEMCİLER SEVKİYAT BİLGİSİ", "SEVKİYAT BİLGİLERİ EKDEDİR", ku_mail_unvani))
            {

                sqlbaglanti.CommScalar("INSERT INTO [TK_SIPARIS_NOTLARI] "
                           + "([not_sipRECno]"
                           + ",[not_kullanici]"
                           + ",[not_detay])"
                           + "VALUES           ( " + secilen_RECno + "," + aktif_kullanici + ",'Teslimat Bilgisi Email Gönderildi.')", CommandType.Text);

            }

            else
                MessageBox.Show("Mail gönderme başarısız");
            mail = new System.Net.Mail.MailMessage();

        }

        private void raporYazdırToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MailFormuYAZDIR();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            MailFormuDZYN();
        }
       
    }
}